/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * TextEditorFrame.java
 *
 * Created on 14/10/2008, 11:21:55 AM
 */
package bradswebdavclient;

import com.ettrema.httpclient.File;
import com.ettrema.httpclient.HttpException;
import com.ettrema.httpclient.Utils.CancelledException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author bradm
 */
public class TextEditorFrame extends javax.swing.JFrame {

    private static final long serialVersionUID = 1L;
    private File file;

    /** Creates new form TextEditorFrame */
    public TextEditorFrame( File f ) throws HttpException, CancelledException {
        initComponents();
        this.file = f;
        load();
    }

    private void load() throws HttpException, CancelledException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        file.download( out, null );
        this.editor.setText( out.toString() );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        editor = new javax.swing.JEditorPane();
        menuBar = new javax.swing.JMenuBar();
        mnuFile = new javax.swing.JMenu();
        mnuSave = new javax.swing.JMenuItem();
        mnuReload = new javax.swing.JMenuItem();
        mnuEdit = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        editor.setName("editor"); // NOI18N
        jScrollPane1.setViewportView(editor);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bradswebdavclient.BradsWebdavClientApp.class).getContext().getResourceMap(TextEditorFrame.class);
        mnuFile.setText(resourceMap.getString("mnuFile.text")); // NOI18N
        mnuFile.setName("mnuFile"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(bradswebdavclient.BradsWebdavClientApp.class).getContext().getActionMap(TextEditorFrame.class, this);
        mnuSave.setAction(actionMap.get("save")); // NOI18N
        mnuSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mnuSave.setText(resourceMap.getString("mnuSave.text")); // NOI18N
        mnuSave.setName("mnuSave"); // NOI18N
        mnuSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnuSaveMouseClicked(evt);
            }
        });
        mnuFile.add(mnuSave);

        mnuReload.setAction(actionMap.get("reload")); // NOI18N
        mnuReload.setText(resourceMap.getString("mnuReload.text")); // NOI18N
        mnuReload.setName("mnuReload"); // NOI18N
        mnuReload.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnuReloadMouseClicked(evt);
            }
        });
        mnuFile.add(mnuReload);

        menuBar.add(mnuFile);

        mnuEdit.setText(resourceMap.getString("mnuEdit.text")); // NOI18N
        mnuEdit.setName("mnuEdit"); // NOI18N
        menuBar.add(mnuEdit);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuReloadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnuReloadMouseClicked
    }//GEN-LAST:event_mnuReloadMouseClicked

    private void mnuSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnuSaveMouseClicked
    }

    @Action
    public void save() {
        System.out.println( "saving.." );
        String s = this.editor.getText();
        System.out.println( "content: " + s );
        ByteArrayInputStream in = new ByteArrayInputStream( s.getBytes() );
        try {
            this.file = this.file.parent.upload( file.name, in, (long) s.length() );
        } catch( Exception ex ) {
            JOptionPane.showMessageDialog( this, "An error occurred saving the resource");
        }
    }//GEN-LAST:event_mnuSaveMouseClicked

    @Action
    public void reload() throws CancelledException {
        System.out.println( "reloading" );
        try {
            load();
        } catch (HttpException ex) {
            Logger.getLogger(TextEditorFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane editor;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu mnuEdit;
    private javax.swing.JMenu mnuFile;
    private javax.swing.JMenuItem mnuReload;
    private javax.swing.JMenuItem mnuSave;
    // End of variables declaration//GEN-END:variables
}
